home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 301-325 / disk_319 / cnewssrc / cnews.orig.lzh / misc / gngp.c < prev    next >
C/C++ Source or Header  |  1989-06-27  |  2KB  |  113 lines

  1. /*
  2.  * gngp - globally find newsgroup and print
  3.  *    like grep, but for newsgroup patterns instead of regular expressions
  4.  */
  5.  
  6. #include <stdio.h>
  7.  
  8. char *progname;
  9. int debug = 0;
  10.  
  11. /*
  12.  * if true, match only ng at start of line, followed by whitespace or newline.
  13.  */
  14. int anchored = 0;
  15.  
  16. FILE *efopen();
  17.  
  18. /*
  19.  * main - parse arguments and handle options
  20.  */
  21. main(argc, argv)
  22. int argc;
  23. char *argv[];
  24. {
  25.     int c, status = 0;
  26.     int errflg = 0;
  27.     FILE *in;
  28.     extern int optind;
  29.     extern char *optarg;
  30.  
  31.     progname = argv[0];
  32.     while ((c = getopt(argc, argv, "ad")) != EOF)
  33.         switch (c) {
  34.         case 'a':        /* anchored at start of line */
  35.             anchored++;
  36.             break;
  37.         case 'd':
  38.             matchdebug(1);    /* all debugging on */
  39.             debug++;
  40.             break;
  41.         default:
  42.             errflg++;
  43.             break;
  44.         }
  45.     if (errflg || optind == argc) {
  46.         (void) fprintf(stderr, "usage: %s [-ad] pattern [file...]\n",
  47.             progname);
  48.         exit(2);
  49.     }
  50.     if (optind == argc-1)
  51.         status |= process(argv[optind], stdin, "stdin");
  52.     else {
  53.         int patind = optind;
  54.  
  55.         for (optind++; optind < argc; optind++) {
  56.             in = efopen(argv[optind], "r");
  57.             status |= process(argv[patind], in, argv[optind]);
  58.             (void) fclose(in);
  59.         }
  60.     }
  61.     exit(status != 0? 0: 1);
  62. }
  63.  
  64. /*
  65.  * process - process input file
  66.  */
  67. process(pattern, in, inname)
  68. char *pattern;
  69. FILE *in;
  70. char *inname;
  71. {
  72.     int status = 0;
  73.     char line[BUFSIZ];
  74.  
  75.     while (fgets(line, sizeof line, in) != NULL)
  76.         if (anchored)
  77.             status |= gngp(pattern, line);
  78.         else {
  79.             register char *start;
  80.  
  81.             for (start = line; *start != '\0'; start++)
  82.                 status |= gngp(pattern, start);
  83.         }
  84.     return status;
  85. }
  86.  
  87. int
  88. gngp(pattern, text)
  89. register char *pattern, *text;
  90. {
  91.     int returned;
  92.     char savewhite;
  93.     char *whitesp;
  94.  
  95.     if (anchored) {
  96.         extern char *strpbrk();
  97.  
  98.         whitesp = strpbrk(text, " \t\n");
  99.         if (whitesp != NULL) {
  100.             savewhite = *whitesp;
  101.             *whitesp = '\0';
  102.         }
  103.     }
  104.     returned = ngmatch(pattern, text);
  105.     if (anchored) {
  106.         if (whitesp != NULL)
  107.             *whitesp = savewhite;
  108.     }
  109.     if (returned)
  110.         (void) fputs(text, stdout);
  111.     return returned;
  112. }
  113.